package ferp.center.server.backend;

import ferp.center.server.Action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: olegs
 * Date: 3/6/13 10:43 PM
 */
public class UpgradeRating extends HttpServlet
{
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    Action.queueTaskForBackend(response, "upgrade rating", "/rating/upgrade");
  }

  @SuppressWarnings("unchecked")
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
/*
    Action.execute(request, response, Void.class, new Action.Task()
    {
      @Override
      protected void run(EntityManager em, HttpServletRequest request) throws IOException
      {
        // the ranker queue has to be paused
        int chunk = 0;


      }

      private List<Rating> next(EntityManager em, int offset)
      {
        return em.createQuery("SELECT r FROM Rating r").setFirstResult(offset).setMaxResults(200).getResultList();
      }

      public Void run(EntityManager em, Void input) throws IOException
      {
        int updated = 0;
        Query q = em.createNamedQuery(Rating.QUERY_GET_ALL);

        List<Rating> ratings = q.getResultList();

        for (Rating r : ratings)
          if (r.getConfiguration().length() == 3)
          {
            StringBuilder sb = new StringBuilder(r.getConfiguration());

            sb.append(Settings.Level.L0.ordinal()).append(Settings.Level.L0.ordinal());
            r.setConfiguration(sb.toString());

            ++updated;

            // todo: remove
            log("updated configuration: id=" + r.getKey() + ", code=" + sb.toString());
          }

        log("ratings upgraded: " + updated + " out of " + ratings.size());

        return null;
      }
    });
*/
  }
}
